from rest_framework import serializers

from users.models import User


class AdminAuthSerializer(serializers.ModelSerializer):
    token = serializers.CharField(label='JWT token', read_only=True)
    # 重定义username.
    username = serializers.CharField(label='用户名')

    class Meta:
        model = User
        fields = ('id', 'username', 'password', 'token')
        extra_kwargs = {
            'password': {
                'write_only': True
            }
        }

    def validate(self, attrs):
        '''判断用户名或者密码是否正确
            attrs : data的数据
        '''
        username = attrs.get('username')
        password = attrs.get('password')

        try:
            # 根据username查询管理员用户
            user = User.objects.get(username=username, is_staff=True)
        except User.DoesNotExist:
            # 管理员用户不存在
            raise serializers.ValidationError('用户名或者密码错误')
        else:
            # 校验密码是都正确
            if not user.check_password(password):
                raise serializers.ValidationError('用户名或者密码错误')

            # 给attrs中添加user属性，保存登录用户
            attrs['user'] = user

        # 注意:必须返回
        return attrs

    def create(self, validated_data):
        # 获取登录用户user
        user = validated_data['user']

        # 服务器生成jwt token, 保存当前用户的身份信息
        from rest_framework_jwt.settings import api_settings

        # 组织payload数据的方法
        jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
        # 生成jwt token数据的方法
        jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER

        # 组织payload数据
        payload = jwt_payload_handler(user)
        # 生成jwt token
        token = jwt_encode_handler(payload)

        # 给user对象增加属性，保存jwt token的数据
        user.token = token

        return user


class UserInfoSerializer(serializers.ModelSerializer):
    '''用户序列化器'''
    class Meta:
        model = User
        fields = ('id', 'username', 'mobile', 'email')
